package com.sxyc.weixin.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class WeixinGetPhoneController  extends Controller {
	public void index(){
		renderJsp("suipai.jsp");
	}

	/**
	 * 获取的随拍数据
	 */
		public void getSuiPai(){
			System.out.println("获取随拍随录资源");
			String type=getPara("type");
			System.out.println("type---->"+type);
			String openid=getPara("openid");
			if(type.equals("image")){
				DateFormat df = new SimpleDateFormat("MM/dd");
				DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				
				calendar.add(Calendar.DATE, 1); //第二天凌晨
				Date date = calendar.getTime();
				String time=df.format(date);
				System.out.println(time);
				
				calendar.add(Calendar.DATE, -1); //当天凌晨
				Date date1 = calendar.getTime();
				String time1=df.format(date1);
				System.out.println(time1);
				
				calendar.add(Calendar.DATE, -1); //前一天凌晨
				Date date2 = calendar.getTime();
				String time2=df.format(date2);
				System.out.println(time2);
				
				calendar.add(Calendar.DATE, -1); //前两天凌晨
				Date date3 = calendar.getTime();
				String time3=df.format(date3);
				System.out.println(time3);
				
				calendar.add(Calendar.DATE, -1); //前三天凌晨
				Date date4 = calendar.getTime();
				String time4=df.format(date4);
				System.out.println(time4);
				
				calendar.add(Calendar.DATE, -1); //前四天凌晨
				Date date5 = calendar.getTime();
				String time5=df.format(date5);
				System.out.println(time5);
				
				System.out.println(date1);
				System.out.println(date2);

				
				String sql="select openId,FileUrl,address,MediaType,CreatedDate from mediaupload where CreatedDate BETWEEN '"+df1.format(date1)+"' and '"+df1.format(date)+"' and  MediaType='image' and OpenId='"+openid+"' ORDER BY CreatedDate DESC";
				List<Record> record1=Db.find(sql);
				Record reccordNow=Db.findFirst("select openId,FileUrl,address,MediaType,CreatedDate from mediaupload where MediaType='image' and openId='"+openid+"' ORDER BY CreatedDate DESC");
				String filepath=reccordNow.get("FileUrl");
				String address=reccordNow.get("address");
				Date createTime=reccordNow.getDate("CreatedDate");
				
				sql="select openId,FileUrl,address,MediaType,CreatedDate from mediaupload where CreatedDate BETWEEN '"+df1.format(date2)+"' and '"+df1.format(date1)+"' and MediaType='image' and openId='"+openid+"' ORDER BY CreatedDate DESC";
				List<Record> record2=Db.find(sql);
				
				sql="select openId,FileUrl,address,MediaType,CreatedDate from mediaupload where CreatedDate BETWEEN '"+df1.format(date3)+"' and '"+df1.format(date2)+"' and MediaType='image' and openId='"+openid+"' ORDER BY CreatedDate DESC ";
				List<Record> record3=Db.find(sql);
				
				List<Record> record4=Db.find("select openId,FileUrl,address,MediaType,CreatedDate from mediaupload where CreatedDate BETWEEN '"+df1.format(date4)+"' and '"+df1.format(date3)+"' and MediaType='image' and openId='"+openid+"' ORDER BY CreatedDate DESC ");
				
				List<Record> record5=Db.find("select openId,FileUrl,address,MediaType,CreatedDate from mediaupload where CreatedDate BETWEEN '"+df1.format(date5)+"' and '"+df1.format(date4)+"' and MediaType='image' and openId='"+openid+"' ORDER BY CreatedDate DESC");
				
				setAttr("time", time);
				setAttr("time1", time1);
				setAttr("time2", time2);
				setAttr("time3", time3);
				setAttr("time4", time4);
				setAttr("time5", time5);
				setAttr("record1", record1);
				setAttr("record2", record2);
				setAttr("record3", record3);
				setAttr("record4", record4);
				setAttr("record5", record5);
				setAttr("filepath", filepath);
				setAttr("address", address);
				setAttr("createTime", createTime);
				renderJsp("suipai.jsp");
			}else if(type.equals("video")){
				String videoPic=getPara("videoPic");
				String sql="select openId,FileUrl,address,MediaType,CreatedDate from mediaupload where MediaType='video' and openId='"+openid+"' ORDER BY CreatedDate DESC LIMIT 2";
				List<Record> videoRecord=Db.find(sql);
				setAttr("videoFilePath1", videoRecord.get(0).get("FileUrl"));
				setAttr("videoTime1", videoRecord.get(0).getDate("CreatedDate"));
				setAttr("videoPic",videoPic);
				renderJsp("suilu.jsp");
			}
			
		}
}
